﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using JXT.PrimaryKey.Batman.Core.Extensions;
using JXT.PrimaryKey.Batman.Core.WcfService;
using JXT.PrimaryKey.Batman.Domain.Models;
using JXT.PrimaryKey.Batman.Domain.Quotation.Models;
using JXT.PrimaryKey.Batman.Domain.Quotation.Services;
using JXT.PrimaryKey.Batman.WcfService.Interfaces;
using JXT.PrimaryKey.Batman.WcfService.Services.Extensions;
using JXT.PrimaryKey.Batman.WcfService.Services.Converters.Quotation;
using DTO = JXT.PrimaryKey.Batman.WcfService.Models.Quotation;

namespace JXT.PrimaryKey.Batman.WcfService.Services
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class WcfQuotationService : WcfWebServiceBase, IQuotation
    {
        public List<DTO.Securities> SearchSecuritiesList(string code, string num)
        {
            if (String.IsNullOrWhiteSpace(num))
            {
                num = "10";
            }
            int intNum = num.ToInt32();
           // var result = LoadService<ISecuritiesService>().GetList(code, intNum);
            //return StockConvert.Instanse.ConvertEnumerable(result).ToList();
            return null;
        }

        public List<DTO.Securities> GetSecuritiesList(string market)
        {
            var list = new List<DTO.Securities>();
            list.Add(new DTO.Securities() { Code = "000002", Name = "万科A", MarketType = DTO.MarketType.Shenzhen | DTO.MarketType.Hongkong, StockType = DTO.StockType.A | DTO.StockType.B });
            return list;
        }


        public DTO.MarketStatistic GetSingle(string code, string day)
        {
            if (code == null)
                throw new ArgumentNullException("code");
            if (day == null)
                throw new ArgumentNullException("day");
            DateTime timeDay = day.ToDateTime();
            var specification = SpecificationFactory.CreateSpecification<MarketStatistic>();
            specification.And(e => e.Code == code);
            specification.And(e => e.UpdateTime == timeDay);
            var entity = LoadService<IMarketStatisticService>().GetSingle(specification);
            return MarketStatisticConverter.Instanse.Convert(entity);
        }

        public IEnumerable<DTO.MarketStatistic> GetList(string code, string firstTime, string lastTime)
        {
            if (code == null)
                throw new ArgumentNullException("code");
            if (firstTime == null)
                throw new ArgumentNullException("firsttime");
            if (lastTime == null)
                throw new ArgumentNullException("lasttime");
            DateTime firstDay = firstTime.ToDateTime();
            DateTime lastDay = lastTime.ToDateTime();
            var specification = SpecificationFactory.CreatePagedSpecification<MarketStatistic>();
            specification.And(e => e.Code == code);
            specification.And(e => e.UpdateTime >= firstDay);
            specification.And(e => e.UpdateTime < lastDay);
            specification.IsEnablePaged = false;
            var list = LoadService<IMarketStatisticService>().GetList(specification);
            return MarketStatisticConverter.Instanse.ConvertEnumerable(list).ToList();
        }
    }
}
